package fan.codeGenerate.analysisXML;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

/** 
* @author liu_fan 2017年3月30日 上午8:54:23  
*/
public class AnalysisXML {
	//数据库链接信息
	private Map<String,Object> jdbcInfo = new HashMap<String,Object>();
	//代码生成信息
	private List<Map<String,Object>> generateList = new ArrayList<Map<String,Object>>();
	//构造方法解析XML文件，将解析出的内容分块放到不同的map当中
	public AnalysisXML(String xmlPath){
		SAXReader saxReader = new SAXReader();
		try {
			Document document = saxReader.read(new File(xmlPath));
			Element rootElement = document.getRootElement();
			Element databaseElement = rootElement.element("database");
			jdbcInfo.put("driver", databaseElement.element("driver").getText());
			jdbcInfo.put("url", databaseElement.element("url").getText());
			jdbcInfo.put("userName", databaseElement.element("userName").getText());
			jdbcInfo.put("password", databaseElement.element("password").getText());
			Element generateElement = rootElement.element("generates");
			List<Element> eleList = generateElement.elements("generate");
			for(Element ele : eleList){
				Map<String,Object> map = new HashMap<String,Object>();
				map.put("tableName", ele.element("tableName").getText());
				map.put("generatePath", ele.element("generatePath").getText());
				generateList.add(map);
			}
		} catch (DocumentException e) {
			e.printStackTrace();
		}
		
	}
	public Map<String, Object> getJdbcInfo() {
		return jdbcInfo;
	}
	public List<Map<String, Object>> getGenerateList() {
		return generateList;
	}
}
